package com.wanglibao.woodtrade.woodtrade.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.ui.activity.SupplyAndDemendActivity;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.HomeFragmentRecommendAdapter;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.HomepageBannerViewPager;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.model.Commodity;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 首页
 * Created by lugg on 2016/5/10.
 */
public class HomeFragment extends BaseFragment {


//    @Bind(R.id.tv_commonTitle_title)
//    TextView tv_commonTitle_title;
//
//    @Bind(R.id.tv_commonTitle_back)
//    TextView tv_commonTitle_back;

    @Bind(R.id.vp_fragment_home_banner)
    ViewPager vp_fragment_home_banner;

    @Bind(R.id.tv_fragment_home_page)
    TextView tv_fragment_home_page;

    @Bind(R.id.ll_fragment_home_sell)
    LinearLayout ll_fragment_home_sell;

    @Bind(R.id.ll_fragment_home_buy)
    LinearLayout ll_fragment_home_buy;

    @Bind(R.id.ll_fragment_home_demand)
    LinearLayout ll_fragment_home_demand;

    @Bind(R.id.list_fragment_home_recommend)
    ListView list_fragment_home_recommend;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this,view);
        initView();
        return view;
    }

    private void initView() {
//        tv_commonTitle_back.setVisibility(View.GONE);
//        tv_commonTitle_title.setText("好木网");

        addViewClicklistener(ll_fragment_home_sell, ll_fragment_home_buy, ll_fragment_home_demand);

        HomepageBannerViewPager homepageBannerViewPager = new HomepageBannerViewPager(getActivity(),
                vp_fragment_home_banner,tv_fragment_home_page,testData());
        homepageBannerViewPager.initScrollView();

        HomeFragmentRecommendAdapter homeFragmentRecommendAdapter = new HomeFragmentRecommendAdapter(this.getContext(),testCommpdity());
        list_fragment_home_recommend.setAdapter(homeFragmentRecommendAdapter);
    }

    private List<Commodity> testCommpdity() {
        List<Commodity> commodityList = new ArrayList<>();

        for (int i = 0; i < 6; i++){
            Commodity commodity = new Commodity();
            commodity.company = "单位"+i;
            commodity.name = "商品名称"+i;
            commodity.price = "价格"+i;
            commodity.specifications = "规格"+i;
            commodity.url = "http://imgtest.meiliworks.com/pic/_o/55/3a/c5f334a8468fda3b1faeb446b08f_640_960.c1.jpg?frm=out_pic";
            commodityList.add(commodity);
        }

        return commodityList;
    }

    private List<String> testData() {
        List<String> imageUrls = new ArrayList<>();
        imageUrls.add("http://easyread.ph.126.net/LHZYBloTRqECwKQK3h5kSg==/7916834464196635983.jpg");
        imageUrls.add("http://www.wmgjad.com/upload/2014/09/23/14114461980345em267.jpg");
        imageUrls.add("http://e.hiphotos.baidu.com/image/pic/item/5bafa40f4bfbfbedef57ab457ff0f736afc31ff9.jpg");
        imageUrls.add("http://imgtest.meiliworks.com/pic/_o/55/3a/c5f334a8468fda3b1faeb446b08f_640_960.c1.jpg?frm=out_pic");
        imageUrls.add("http://img1.gtimg.com/ent/pics/hv1/196/22/1963/127649881.png");
        return imageUrls;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.ll_fragment_home_sell:
                ToastUtil.createNormalToast(getActivity(),"卖货");
                break;
            case R.id.ll_fragment_home_buy:
                ToastUtil.createNormalToast(getActivity(),"买货");
                break;
            case R.id.ll_fragment_home_demand:
                startActivity(new Intent(getActivity(), SupplyAndDemendActivity.class));
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (this.getView() != null)
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
    }
}
